package com.neueedu.android.concat;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;


//单独导包
import com.neueedu.android.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


public class ConcatEditActitiy extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private List<Map<String,Object>> data = new ArrayList<>();
    SimpleAdapter adapter;

    HashMap<String, Object> row = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_concat_edit_actitiy);
        setTitle("编辑联系人");

        Intent intent = getIntent();
        row  = (HashMap<String, Object>) intent.getSerializableExtra("row");

        //1 找到  ListView
        ListView lvConcatEditList = findViewById(R.id.lvConcatEditList);


        //准备数据
        initData();

        //创建适配器并绑定该数据

        String[] from =  new String[]{"key1","key2","key3"};
        int to[] = new int[]{R.id.tvKey1 ,R.id.tvKey2,R.id.ivKey3 };
        adapter = new SimpleAdapter(this, data, R.layout.concat_edit_list_item, from, to);



        //3 将适配器和 ListView绑定
        lvConcatEditList.setAdapter(adapter);


        lvConcatEditList.setOnItemClickListener(this);

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position){
            case 0:  //修改姓名
                showChangeNameDialog();
                break;

            case  3:
                showChangeTypeDialog();
                break;
            case  4: //选择出生日期
                showChangeBirthdayDialog();
                break;
        }



    }

    //选择日期
    private void showChangeBirthdayDialog() {


        DatePickerDialog  datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {


                String str = "year:"+year+",month: "+month+", day:"+dayOfMonth;

                Log.d("onDateSet",year+"");
                Log.d("onDateSet",month+"");
                Log.d("onDateSet",dayOfMonth+"");


                //Toast
                Toast.makeText(ConcatEditActitiy.this, str, Toast.LENGTH_LONG).show();

            }
        },2024,4,22);



        datePickerDialog.show();

    }

    private void showChangeTypeDialog() {
        //弹出对话框， 内容部分显示一个  EditText
        final  String types[] = new String[]{"成人","学生","老人","军人"};
        Dialog dialog  = new AlertDialog.Builder(this).setTitle("修改姓名")

                .setSingleChoiceItems(types, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Map<String, Object> row1 = data.get(3);
                        row1.put("key2",types[which]); //仅修改了内存的数据


                        //使用适配器通知数据发生变更，重新渲染
                        adapter.notifyDataSetChanged();

                        dialog.dismiss();
                    }
                }).show();
    }

    private void showChangeNameDialog() {

        //弹出对话框， 内容部分显示一个  EditText
        //通过JAVA程序 创建的 编辑框
        final  EditText  view = new EditText(this);//可以输入的文本框

        //通过布局文件加载一个

//        LayoutInflater inflater = LayoutInflater.from(this);
//        View v = inflater.inflate(R.layout.concat_edit_list_item ,null);

        view.setText((String) row.get("name"));
        Dialog dialog  = new AlertDialog.Builder(this).setTitle("修改姓名")
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        Map<String, Object> row1 = data.get(0);
                        row1.put("key2",view.getText().toString()); //选中的类型


                        //使用适配器通知数据发生变更，重新渲染
                        adapter.notifyDataSetChanged();


                    }
                }).show();

    }

    private void initData() {


        String name = (String) row.get("name");
        String idno = (String) row.get("idno");



        Map<String,Object> row1= new HashMap();
        row1.put("key1","姓名");
        row1.put("key2",name);
        row1.put("key3",R.drawable.forward_25);

        Map<String,Object> row2= new HashMap();
        row2.put("key1","证件号码");
        row2.put("key2",idno);


        Map<String,Object> row3= new HashMap();
        row3.put("key1","性别");
        row3.put("key2","男");
        row3.put("key3",R.drawable.forward_25);

        Map<String,Object> row4= new HashMap();
        row4.put("key1","乘客类型");
        row4.put("key2","成人");
        row4.put("key3",R.drawable.forward_25);

        Map<String,Object> row5= new HashMap();
        row5.put("key1","出生日期");
        row5.put("key2","2023-03-16");
        row5.put("key3",R.drawable.forward_25);

        data.add(row1);
        data.add(row2);
        data.add(row3);
        data.add(row4);
        data.add(row5);
    }


}
